package com.javaweb.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.javaweb.annotation.token.TokenDataAnnotation;
import com.javaweb.annotation.url.ControllerMethod;
import com.javaweb.base.BaseResponseResult;
import com.javaweb.base.BaseService;
import com.javaweb.base.po.Role;
import com.javaweb.constant.ApiCommonConstant.RoleConstant;
import com.javaweb.db.query.QueryWapper;
import com.javaweb.enums.AuthEnum;
import com.javaweb.enums.HttpCodeEnum;
import com.javaweb.util.core.ObjectOperateUtil;
import com.javaweb.util.entity.Page;
import com.javaweb.web.eo.TokenData;
import com.javaweb.web.eo.role.ModuleInfoResponse;
import com.javaweb.web.eo.role.RoleAddRequest;
import com.javaweb.web.eo.role.RoleDetailResponse;
import com.javaweb.web.eo.role.RoleListAllResponse;
import com.javaweb.web.eo.role.RoleListRequest;
import com.javaweb.web.eo.role.RoleModifyRequest;

/**
 * 登录且需要权限才可访问的角色管理模块
 * <p>
 * 该模块主要提供角色管理相关的接口
 * 
 * @author tank2140896
 */
@RestController
@RequestMapping(RoleConstant.WEB_ROLE_PREFIX)
public class RoleController extends BaseService {
	
	/**
	 * 新增角色接口
	 * 
	 * @param roleAddRequest 角色新增请求参数
	 * @param tokenData tokenData
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@PostMapping(RoleConstant.ROLE_ADD)
	@ControllerMethod(interfaceName="新增角色接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult roleAdd(@RequestBody @Validated RoleAddRequest roleAddRequest,@TokenDataAnnotation TokenData tokenData){
		/** 新增角色-字段唯一性校验 start */
		List<QueryWapper<Role>> queryWappers = List.of(
			new QueryWapper<Role>().eq(Role::getRoleName,roleAddRequest.getRoleName()).eq(Role::getDelFlag,0),
			new QueryWapper<Role>().eq(Role::getRoleCode,roleAddRequest.getRoleCode()).eq(Role::getDelFlag,0)
		);
		List<String> messages = List.of("validated.role.roleName.repeat","validated.role.roleCode.repeat");
		validateService.columnUniqueCheck(roleDao,queryWappers,messages);
		/** 新增角色-字段唯一性校验 end */
		roleService.roleAdd(tokenData.getUser(),roleAddRequest);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"role.add.success");
	}
	
	/**
	 * 查询角色接口
	 * 
	 * @param roleListRequest 角色列表请求参数
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@PostMapping(RoleConstant.ROLE_LIST)
	@ControllerMethod(interfaceName="查询角色接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult roleList(@RequestBody RoleListRequest roleListRequest){
		Page page = roleService.roleList(roleListRequest);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"role.list.success",page);
	}
	
	/**
	 * 修改角色接口
	 * 
	 * @param roleModifyRequest 角色修改请求参数
	 * @param tokenData tokenData
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@PutMapping(RoleConstant.ROLE_MODIFY)
	@ControllerMethod(interfaceName="修改角色接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult roleModify(@RequestBody @Validated RoleModifyRequest roleModifyRequest,@TokenDataAnnotation TokenData tokenData){
		Role originRole = roleService.roleDetail(roleModifyRequest.getRoleId());
		if(originRole==null){
			return getBaseResponseResult(HttpCodeEnum.VALIDATE_ERROR,"validated.role.modify.notExist");
		}
		/** 修改角色-字段唯一性校验 start */
		List<QueryWapper<Role>> queryWappers = List.of(
			new QueryWapper<Role>().eq(Role::getRoleName,roleModifyRequest.getRoleName()).eq(Role::getDelFlag,0).neq(Role::getRoleId,roleModifyRequest.getRoleId()),
			new QueryWapper<Role>().eq(Role::getRoleCode,roleModifyRequest.getRoleCode()).eq(Role::getDelFlag,0).neq(Role::getRoleId,roleModifyRequest.getRoleId())
		);
		List<String> messages = List.of("validated.role.roleName.repeat","validated.role.roleCode.repeat");
		validateService.columnUniqueCheck(roleDao,queryWappers,messages);
		/** 修改角色-字段唯一性校验 end */
		roleService.roleModify(tokenData.getUser(),roleModifyRequest);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"role.modify.success");
	}
	
	/**
	 * 角色详情接口
	 * 
	 * @param roleId 角色ID
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@GetMapping(RoleConstant.ROLE_DETAIL)
	@ControllerMethod(interfaceName="角色详情接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult roleDetail(@PathVariable(name="roleId",required=true) String roleId){
		Role role = roleService.roleDetail(roleId);
		if(role==null){
			return getBaseResponseResult(HttpCodeEnum.VALIDATE_ERROR,"validated.role.notExist");
		}
		RoleDetailResponse roleDetailResponse = new RoleDetailResponse();
		ObjectOperateUtil.copyProperties(role,roleDetailResponse);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"role.detail.success",roleDetailResponse);
	}
	
	/**
	 * 删除角色接口（支持批量删除，用逗号隔开）
	 * 
	 * @param roleId 角色ID
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@DeleteMapping(RoleConstant.ROLE_DELETE)
	@ControllerMethod(interfaceName="删除角色接口（支持批量删除，用逗号隔开）",auth=AuthEnum.PERMISSION)
	public BaseResponseResult roleDelete(@PathVariable(name="roleId",required=true) String roleId){
		roleService.roleDelete(roleId);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"role.delete.success");
	}
	
	/**
	 * 角色模块信息接口
	 * 
	 * @param roleId 角色ID
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@GetMapping(RoleConstant.ROLE_MODULE_INFO)
	@ControllerMethod(interfaceName="角色模块信息接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult roleModuleInfo(@PathVariable(name="roleId",required=true) String roleId,
			                                 @PathVariable(name="platformName",required=true) String platformName){
		Map<String,String> map = new HashMap<>();
		map.put("roleId",roleId);
		map.put("platformName",platformName);
		List<ModuleInfoResponse> list = roleService.roleModuleInfo(map);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"role.roleModuleInfo.success",list);
	}
	
	/**
	 * 角色模块分配接口
	 * 
	 * @param list 角色模块列表
	 * @param roleId 角色ID
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@PostMapping(RoleConstant.ROLE_MODULE_ASSIGNMENT)
	@ControllerMethod(interfaceName="角色模块分配接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult roleModuleAssignment(@RequestBody List<String> list,@PathVariable(name="roleId",required=true) String roleId){
		roleService.roleModuleAssignment(roleId,list);
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"role.roleModuleAssignment.success");
	}
	
	/**
	 * 角色列表接口
	 * 
	 * @return BaseResponseResult 接口通用返回参数
	 */
	@GetMapping(RoleConstant.ROLE_LIST_ALL)
	@ControllerMethod(interfaceName="角色列表接口",auth=AuthEnum.PERMISSION)
	public BaseResponseResult roleListAll(){
		List<RoleListAllResponse> list = roleService.roleListAll();
		return getBaseResponseResult(HttpCodeEnum.SUCCESS,"role.roleListAll.success",list);
	}
	
}
